package stream

import "net"

// DownStream 下行
func DownStream(client, remote net.Conn) {
	defer client.Close()
	defer remote.Close()

	buff := make([]byte, 1024)
	for {
		r, err := remote.Read(buff)
		if err != nil {
			if ne, ok := err.(net.Error); ok && ne.Temporary() {
				continue
			}
			return
		}
		w := 0
		for w < r {
			n, err := client.Write(buff[:r])
			if err != nil {
				if ne, ok := err.(net.Error); ok && ne.Temporary() {
					continue
				}
				return
			}
			w += n
		}
	}
}
